/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package py.com.erpone.session;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import py.com.erpone.entity.dm.Usuario;
import py.com.erpone.util.LocaleSelector;



/**
 *
 * @author curso
 */
@Stateless
@Named
public class LoginBean {

    private String nombre;
    private String password;
    @PersistenceContext
    private EntityManager em;
    @Inject
    private LocaleSelector localeSelector;

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String login() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        
        
        try {       	
            request.login(nombre, password);            
        } catch (ServletException ex) {
            ex.printStackTrace();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, localeSelector.getMensaje("login_incorrecto"), nombre));
            return "login.xhtml";
        }
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, localeSelector.getMensaje("login_exito"), nombre));
        return "welcome.xhtml";
    }

    public String logout() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        try {
            request.logout();
        } catch (ServletException ex) {
            context.addMessage(null, new FacesMessage("Error"));
        }
        return "/login.xhtml";
    }

    public void registrar() throws NoSuchAlgorithmException {
        FacesContext context = FacesContext.getCurrentInstance();
        Usuario u = new Usuario();
        u=em.find(Usuario.class, nombre);
        if(u!=null){
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, localeSelector.getMensaje("usuario_existente"), nombre));
            return;
        }
        MessageDigest sha = MessageDigest.getInstance("SHA-512");
        byte[] hashPassword = sha.digest(password.getBytes());

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < hashPassword.length; i++) {
            sb.append(Integer.toString((hashPassword[i] & 0xff) + 0x100, 16).substring(1));
        }
        
        u=new Usuario();
        
        u.setNombreUsuario(nombre);
        //u.setPassword(sb.toString().toUpperCase());
        u.setPassword(sb.toString());
        //Grupo socio = em.find(Grupo.class, "SOCIO");

        //u.setGrupo(new ArrayList<Grupo>());
        //u.getGrupo().add(socio);


        em.persist(u);
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, localeSelector.getMensaje("usuario_registrado"), nombre));

        

    }
}
